// const { SuccessModel, ErrorModel } = require("./model/responseModel");
const { get_hotel_daily_settel, insert_card_for_power, getCardListdOfHotel, } = require("../controllers/controlDB");
var { rent_hotel_data, occupancy_rate, rental_dealing, ranking_list, occupancy_rental, occupancy_rental2, time_transition, install_room } = require('../controllers/controlDB2')
var { hotel_list_show, rent_hotel_data2, option_occupancyAll, option_occupancy, option_rental, hotel_data_proportion } = require('../controllers/controlDB3')
var { insert_valid_invalid, lastQueryTime, readinvalid, readinvalid2, card_data } = require('../controllers/controlDB4')
// const { hotel_room_card } = require("./entity");

//处理相关的路由
var lastTimes;
function handleRoute(req, res){
    var method = req.method;
    //定义处理路由的逻辑
    if (method === "GET") {//处理GET请求
        console.log("path:", req.path);
        console.log("query:", req.query);
        if (req.path == '/settlement') {//根据时间段查询6家酒店信息，结算
            if(!req.query.time) return "参数time不存在";
            var date = time_transition(req.query.time);
            var listDataPromise = get_hotel_daily_settel(date[0], date[1]);
            return listDataPromise.then((data) => {//返回一个含两个数组的数组
                // console.log(data)
                return data;
            });
        } else if (req.path == '/insert_card_for_power') {//进行插卡取电查询
            var time = req.query.range_time_picker.split(',');
            var listDataPromise = insert_card_for_power(time[0], time[1]);
            // var hotelRoomData=hotel_room_card2;//将酒店房间对应registerId存为实体，读数据库耗时高
            return listDataPromise.then(data => {
                // console.log(data);
                return data;
            });
        } else if (req.path == '/hotel') {//根据酒店名或Id获取所有房间卡
            var hotel = null;
            if (req.query.hotelName) hotel = req.query.hotelName;//名字或Id
            else hotel = parseInt(req.query.hotelId);
            var listDataPromise = getCardListdOfHotel(hotel);
            return listDataPromise;//根据酒店名或Id获取所有房间卡

        } else if (req.path == '/rent_number') {//任务2 合同内酒店数据
            return rent_hotel_data().then(data => {
                // console.log(data)
                return data;
            });
        } else if (req.path == '/occupancy_rate') {//总入住率
            var listDataPromise = occupancy_rate();
            console.log('执行了occupancy_rate');
            return listDataPromise;
        } else if (req.path == '/rental_dealing') {//酒店租金数据
            var listDataPromise = rental_dealing();
            return listDataPromise;
        } else if (req.path == '/ranking_list') {//各项数据排行榜
            var listDataPromise = ranking_list();
            console.log('执行了ranking_list');
            return listDataPromise;
        } else if (req.path == '/occupancy_rental') {//入住和租金展示，租金部分待修改
            if (!req.query.time) return `缺少参数time，请添加`;
            var date = time_transition(req.query.time);
            if(req.query.time==='昨日'){//选中昨日却要7天的数据
                date[1]=date[0];
                date[0]=new Date(date[0]);
                date[0].setDate(date[0].getDate() - 7);
                date[0] = date[0].getFullYear() + '-' + (date[0].getMonth() + 1) + '-' + date[0].getDate() + ' 23:59:59';
            }
            var listDataPromise = occupancy_rental(date[0], date[1]);//入住率//收租
            return listDataPromise;
        } else if (req.path == '/occupancy_rental2') {//任务2入住和租金展示,收租部分
            var date = time_transition(req.query.time);
            var listDataPromise = occupancy_rental2(date[0], date[1]);
            return listDataPromise;
        } else if (req.path == '/install_room') {//任务2 安装房间部分部分
            var date = time_transition(req.query.time);
            if(req.query.time==='昨日'){//选中昨日却要7天的数据
                date[1]=date[0];
                date[0]=new Date(date[0]);
                date[0].setDate(date[0].getDate() - 7);
                date[0] = date[0].getFullYear() + '-' + (date[0].getMonth() + 1) + '-' + date[0].getDate() + ' 23:59:59';
            }
            return install_room(date[0], date[1]);
        } else if (req.path == '/hotel_list_show') {//任务3酒店列表综述
            return hotel_list_show().then(data => {
                var arrs = [];
                for (var key in data) arrs.push({
                    'hotelId': key,
                    'name': data[key].name, 'roomnum': data[key].roomnum, 'isRent': data[key].isRent
                });
                return arrs;
            });
        } else if (req.path == '/rent_hotel_data') {//酒店综述
            var hotellist = req.query.hotellist || '';
            // console.log("321:",hotellist)
            return rent_hotel_data2(hotellist);
        } else if (req.path == '/option_occupancyAll') {//总入住率
            var hotellist = req.query.hotellist;
            return option_occupancyAll(hotellist);
        } else if (req.path == '/option_rental_dealing') {//总收租
            var hotellist = req.query.hotellist;
            return rental_dealing(hotellist);
        } else if (req.path == '/option_occupancy') {//选中酒店入住
            var hotellist = req.query.hotellist;
            var date = time_transition(req.query.time.trim());//去除字符串两端空白
            if(req.query.time==='昨日'){//选中昨日却要7天的数据
                date[1]=date[0];
                date[0]=new Date(date[0]);
                date[0].setDate(date[0].getDate() - 7);
                date[0] = date[0].getFullYear() + '-' + (date[0].getMonth() + 1) + '-' + date[0].getDate() + ' 23:59:59';
            }
            return option_occupancy(hotellist, date[0], date[1]);
        } else if (req.path == '/option_rental') {//选中酒店收租
            var hotellist = req.query.hotellist;
            var date = time_transition(req.query.time);
            // console.log('option_rental',date)
            return option_rental(hotellist, date[0], date[1])
        } else if (req.path == '/hotel_data_proportion') {//酒店各项数据占比
            var hotellist = req.query.hotellist;
            var date = time_transition(req.query.time);
            return hotel_data_proportion(hotellist, date[0], date[1]);
        } else if (req.path == '/insert_valid_invalid') {//任务4各酒店插卡取电详情
            if (!lastTimes) lastTimes = lastQueryTime();//undefined
            var hotellist = req.query.hotellist;
            var date = time_transition(req.query.time);
            return insert_valid_invalid(hotellist, date[0], date[1]);
        } else if (req.path == '/readinvalid') {//任务4插卡无效详情
            var hotellist = req.query.hotellist;
            var date = time_transition(req.query.time);
            return readinvalid(hotellist, date[0], date[1]);
        } else if (req.path == '/readinvalid2') {//任务4插卡有效加无效详情
            var hotellist = req.query.hotellist;
            var date = time_transition(req.query.time);
            return readinvalid2(hotellist, date[0], date[1]);
        } else if (req.path == '/card_data') {//根据酒店房间regiId和时间查询插卡次数和持续时间
            var regiId = req.query.registerId;
            var date = time_transition(req.query.time);
            return card_data(regiId, date[0], date[1]).then(data=>{
                // console.log(data);
                return data;
            });
        }

        console.log(`GET请求里暂时没有对path=${req.path}作定义！`);
        // return `GET请求里暂时没有对path=${req.path}作定义！`;
        return new Promise((resolve, reject) => { resolve(`GET请求里暂时没有对path=${req.path}作定义！`) });
    } else if (method === "POST") {//处理POST请求
        console.log("body:", req.body);
        console.log("POST请求暂时还没有设计！");
        return "POST请求暂时还没有设计！";
    } else {
        console.log("既不是GET请求，也不是POST请求！");
        return "既不是GET请求，也不是POST请求！";
    }
}
module.exports ={
    handleRoute,
    lastTimes
}